package cn.itcast.flink.source.test;

import cn.itcast.flink.source.test.entity.VehicleBeanPlus;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Author itcast
 * Date 2021/9/19 17:23
 * + 数组等复杂类型解析
 */
public class JsonPlusParse {
    public static void main(String[] args) {
        String json = "{\"batteryAlarm\": 0,\"carMode\": 1,\"minVoltageBattery\": 3.89,\"chargeStatus\": 1,\"vin\": \"LS5A3CJC0JF890971\",\"nevChargeSystemTemperatureDtoList\": [{\"probeTemperatures\": [25, 23, 24, 21, 24, 21, 23, 21, 23, 21, 24, 21, 24, 21, 25, 21],\"chargeTemperatureProbeNum\": 16,\"childSystemNum\": 1}]}";
        //2.创建一个pojo类
        JSONObject jsonObject = new JSONObject(json);
        //初始化 List
        List<Integer> probeTempList = new ArrayList<>();
        //3.使用org.gson 解析json字符串
        int batteryAlarm = jsonObject.getInt("batteryAlarm");
        int carMode = jsonObject.getInt("carMode");
        double minVoltageBattery = jsonObject.getDouble("minVoltageBattery");
        int chargeStatus = jsonObject.getInt("chargeStatus");
        String vin = jsonObject.getString("vin");
        //获取探针的温度的数组列表
        JSONArray nevChargeSysTemDtoList = jsonObject.getJSONArray("nevChargeSystemTemperatureDtoList");
        //获取第一个数组中的值
        String firstArrJson = nevChargeSysTemDtoList.get(0).toString();
        //将json字符串转换成对象
        JSONObject nevChargeSysTempObj = new JSONObject(firstArrJson);
        //将值读取出来
        int chargeTemperatureProbeNum = nevChargeSysTempObj.getInt("chargeTemperatureProbeNum");
        int childSystemNum = nevChargeSysTempObj.getInt("childSystemNum");
        //将探针温度列表值取出来并封装为 List
        JSONArray probeTemperatureArr = nevChargeSysTempObj.getJSONArray("probeTemperatures");

        for (Object pt : probeTemperatureArr) {
            probeTempList.add(Integer.parseInt(pt.toString()));
        }

        VehicleBeanPlus vehicleBean = new VehicleBeanPlus(
                batteryAlarm
                ,carMode
                ,minVoltageBattery
                ,chargeStatus
                ,vin
                ,probeTempList
                ,chargeTemperatureProbeNum
                ,childSystemNum
        );

        System.out.println(vehicleBean.toString());
    }

}
